package edu.buaa.act.travel.m.model;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONObject;

import edu.buaa.act.travel.m.Constants;
import edu.buaa.act.travel.m.utils.JSONParser;

public class RunModel implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 329389474055467836L;
	
	private String mCurrentItem;
	private String mState;
	private String mQueryText;
	private String[] mQueryOptions;
	
	public RunModel(final String result) {
		final JSONObject jo = JSONParser.getJSONObject(result);
		final JSONObject info = JSONParser.getJSONObject(jo, Constants.KEY_RUN_INFO);
		mCurrentItem = JSONParser.getString(info, Constants.KEY_RUN_ITEM);
		mState = JSONParser.getString(info, Constants.KEY_STATE);
		
		final int state = getState();
		if(state != -1 && state != 0) {
			final JSONObject attach = JSONParser.getJSONObject(jo, Constants.KEY_ATTACH_INFO);
			mQueryText = JSONParser.getString(attach, Constants.KEY_QUERY_TEXT);
			final JSONArray options = JSONParser.getJSONArray(attach, Constants.KEY_QUERY_OPTION);
			final int size = options.length();
			mQueryOptions = new String[size];
			for(int i = 0; i < size; i++) {
				mQueryOptions[i] = JSONParser.getString(options, i);
			}
		}
	}
	
	public int getCurrentItem() {
		try {
			return Integer.valueOf(mCurrentItem);
		} catch(Exception e) {
			return -1;
		}
	}
	
	public int getState() {
		try {
			return Integer.valueOf(mState);
		} catch(Exception e) {
			return -1;
		}
	}
	
	public String getQueryText() {
		return mQueryText;
	}
	
	public String[] getQueryOptions() {
		return mQueryOptions;
	}
}
